﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace VirtualTournaments.Frontend.MenuControls.TournamentManagerMenu
{
    using Team = Services.Tournaments.Team;
    using TeamsService = Services.Tournaments.TeamsService;
    using Tournament = Services.Tournaments.Tournament;
    using TournamentsService = Services.Tournaments.TournamentsService;
    using System.Collections.Generic;

    public partial class CreateTournamentMenu : System.Web.UI.UserControl
    {
        public EventHandler GoBackButtonClicked;

        protected void Page_Load(object sender, EventArgs e) 
        {
            if (!IsPostBack)
            {
                Team[] teamsInTournament = TeamsService.GetAllTeams();

                foreach (Team team in teamsInTournament)
                    AvailableTeamsListBox.Items.Add(team.Name);
            }
        }

        protected void TournamentTypeRadioButtonList_OnSelectedIndexChanged(object sender, EventArgs e) 
        {
            ListItemCollection optionItems = (sender as RadioButtonList).Items;
            TournamentTeamCountDropDownList.Items.Clear();
            if (optionItems[0].Selected)
                FillAllAgainstAllTeamCount();
            else
                FillPlayOffTeamCount();
       }

        private void FillAllAgainstAllTeamCount()
        {
            for (int i = 3; i <= 20; i++)
                TournamentTeamCountDropDownList.Items.Add(i.ToString());
        }

        private void FillPlayOffTeamCount() 
        {
            for (int i = 2; i <= 5; i++)
                TournamentTeamCountDropDownList.Items.Add((Math.Pow(2,i)).ToString());
        }

        protected void GoBackButton_OnClick(object sender, EventArgs e) 
        {
            GoBackButtonClicked(this, new VirtualTournamentsFrontendEvent { SenderMenu = "CreateTournamentMenu" });
        }

        protected void AddTeamButton_OnClick(object sender, EventArgs e) 
        {
            int teamCount = Convert.ToInt32(TournamentTeamCountDropDownList.SelectedValue);

            if ( (AvailableTeamsListBox.SelectedIndex >= 0) && (SelectedTeamsListBox.Items.Count < teamCount) ) 
            {
                SelectedTeamsListBox.Items.Add(AvailableTeamsListBox.SelectedValue);
                AvailableTeamsListBox.Items.RemoveAt(AvailableTeamsListBox.SelectedIndex);

                if (SelectedTeamsListBox.Items.Count == teamCount)
                    CreateTournamentConfirmButton.Enabled = true;
            }
        }

        protected void RemoveTeamButton_OnClick(object sender, EventArgs e) 
        {
            int teamCount = Convert.ToInt32(TournamentTeamCountDropDownList.SelectedValue);

            if (SelectedTeamsListBox.SelectedIndex >= 0)
            {
                AvailableTeamsListBox.Items.Add(SelectedTeamsListBox.SelectedValue);
                SelectedTeamsListBox.Items.RemoveAt(SelectedTeamsListBox.SelectedIndex);

                if (SelectedTeamsListBox.Items.Count < teamCount)
                    CreateTournamentConfirmButton.Enabled = false;
            }
        }

        protected void CreateTournamentConfirmButton_OnClick(object sender, EventArgs e)
        {
            //TODO: validate tournament name!!!
            if (TournamentNameTextBox.Text.Equals(string.Empty))
                return;
            if (TournamentTypeRadioButtonList.SelectedIndex < 0)
                return;
            
            string tType = string.Empty;
            if (TournamentTypeRadioButtonList.SelectedIndex == 0)
                tType = Tournament.ALLAGAINSTALL_TYPE;
            else
                tType = Tournament.PLAYOFF_TYPE;

            Tournament newTournament = new Tournament(TournamentNameTextBox.Text, tType);
            List<Team> teamsInTournament = new List<Team>();
            Team newTeam;
            foreach (ListItem teamItem in SelectedTeamsListBox.Items) 
            {
                newTeam = new Team(teamItem.Value);
                teamsInTournament.Add(newTeam);
            }

            //TournamentsService.CreateTournament(teamsInTournament.ToArray(), newTournament);
        }

        protected void CreateTournamentCancelButton_OnClick(object sender, EventArgs e)
        {
            GoBackButtonClicked(this, new VirtualTournamentsFrontendEvent { SenderMenu = "CreateTournamentMenu" });
        }
    }
}